<?php

namespace app\api\controller;

use app\admin\model\product\Product;
use app\common\controller\Api;

/**
 * diy配置列表
 */
class Diy extends Api
{
    protected $noNeedRight = ['*'];


    //添加配置-CPU,主板,电源,散热,机箱只能添加一个,其他可以多个
    public function add()
    {
        try {
            $params = input();

            $params['user_id'] = $this->auth->id;
            $diy_model = new \app\api\model\Diy();
            $res = $diy_model->add($params);
        }  catch (\Exception $e) {
            $this->error($e->getMessage());
        }

        $this->success('请求成功',$res);
    }

    //配置详情
    public function detail()
    {
        $id = input('id',0);

        $info = \app\api\model\Diy::get($id);
        $ginfo = [];
        $content = json_decode($info['content'],true);
        //dump($content);die;
        foreach ($content as $kk=>$vv){
            $product = Product::get($vv['goods_id']);
            array_push($ginfo,$product);
        }

        $info['ginfo'] = $ginfo;


        $this->success('请求成功',$info);
    }


    //配置删除
    public function del()
    {
        $id = input('id',0);

        $info = \app\api\model\Diy::get($id);
        $info->delete();
        $this->success('请求成功');
    }

    //配置列表
    public function lists()
    {
        $params = input();

        $params['user_id'] = $this->auth->id;
        $diy_model = new \app\api\model\Diy();
        $res = $diy_model->getLists($params);
        $this->success('请求成功',$res);
    }

    //校验
    public function check_jrx()
    {
        $params = input();

        //$params['user_id'] = $this->auth->id;
        $diy_model = new \app\api\model\Diy();
        $res = $diy_model->check_jrx($params);
        $this->success('请求成功',$res);
    }


    //海报
    public function poster()
    {
        $page = input('page','');

        $shop_id = input('diy_id',0);

        $app_type = input('app_type','weapp');

        $qrcode_param = input('qrcode_param','');//二维码

        if (empty($qrcode_param)) {
            $this->error('缺少二维码参数');
        }

        $qrcode_param = json_decode($qrcode_param, true);

        $qrcode_param[ 'suid' ] = $this->auth->id;

        $qrcode_param[ 'diy_id' ] = $shop_id;
        try {

            $model = new \app\api\model\Diy();

            $res = $model->poster($page,$qrcode_param,$app_type);

        } catch (\Exception $e) {
            $this->error($e->getMessage());
        }
        $this->success('',$res);
    }

}